﻿using System;
using System.Collections.Generic;
using System.Text;
using llAma.Backend.Interface;
using llAma;
using MySql.Data.MySqlClient;

namespace socialdeveloperBackend
{
    public static class GlobalUserCache
    {
        private static ServerCache<string, IUserData> userCacheByName = new ServerCache<string, IUserData>(new TimeSpan(0, 0, 15));
        private static ServerCache<int, IUserData> userCacheById = new ServerCache<int, IUserData>(new TimeSpan(0, 0, 15));
        static CacheEntry<IUserData> OnMissByName(string name)
        {
            using (MySqlConnection conn = SDBackend.CreateConnection())
            {
                using (MySqlDataReader dr = SDBackend.ExecuteReader(conn, "SELECT * FROM tbl_users WHERE username = ?id", new MySqlParameter("id", name)))
                {
                    if (dr.Read())
                        return new CacheEntry<IUserData>(new Userdata(dr));
                    return new CacheEntry<IUserData>(null);
                }
            }
        }
        static CacheEntry<IUserData> OnMissByID(int id)
        {
            using (MySqlConnection conn = SDBackend.CreateConnection())
            {
                using (MySqlDataReader dr = SDBackend.ExecuteReader(conn, "SELECT * FROM tbl_users WHERE id = ?id", new MySqlParameter("id", id)))
                {
                    if (dr.Read())
                        return new CacheEntry<IUserData>(new Userdata(dr));
                    return new CacheEntry<IUserData>(null);
                }
            }
        }
        static GlobalUserCache()
        {
            userCacheByName.CacheMiss = new ServerCache<string, IUserData>.CacheMissDelegate(OnMissByName);
            userCacheById.CacheMiss = new ServerCache<int, IUserData>.CacheMissDelegate(OnMissByID);
        }
        public static void BuildInitialCache()
        {
            using (MySqlConnection conn = SDBackend.CreateConnection())
            {
                using (MySqlDataReader dr = SDBackend.ExecuteReader(conn, "SELECT * FROM tbl_users"))
                {
                    int i = 0;
                    while (dr.Read())
                    {
                        IUserData d = new Userdata(dr);
                        userCacheById.SetCacheObject(int.Parse(d.getInfo("id")), d);
                        userCacheByName.SetCacheObject(d.getInfo("name").ToLower(), d);
                        i++;
                    }
                }
            }
        }
        public static void DropUser(IUserData u)
        {
            userCacheById.DeleteCacheObject(int.Parse(u.getInfo("id")));
            userCacheByName.DeleteCacheObject(u.getInfo("username").ToLower());
        }
        public static IUserData GetUser(int id)
        {
            return userCacheById.GetCacheObject(id);
        }
        public static IUserData GetUser(string name)
        {
            return userCacheByName.GetCacheObject(name.ToLower());
        }
        public static bool Authenticate(IUserData user, string token)
        {
            return (user.getInfo("token").ToLower() == token.ToLower());
        }
    }
    public partial class SDBackend
    {
    }
}
